﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class FareImporting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                lblError.Text = "";
                return;
            }
            else
            {
                if (Session["ACL"].ToString().IndexOf("FG1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }

            }
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FG2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            int ret_code;

            try
            {
                cmd.Parameters.Clear();
                cmd.CommandText = "bll_fare_add_authorized_import_airline";
                para = cmd.Parameters.AddWithValue("@importer", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@exporter", lstAirlines.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                {
                    lblError.Text = "Adding failed!";
                }
                else if (ret_code == -1)
                {
                    lblError.Text = "Done. But saving log failed!";
                    GridView1.DataBind();
                }
                else
                {
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FG2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string key;
            SqlCommand cmd = new SqlCommand();
            int ret_code;
            lblError.Text = "";
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a record.";
                return;
            }
            key = Request.Form["KEY"];
            try
            {
                cmd.CommandText = "bll_fare_remove_authorized_import_airline";
                SqlParameter para = cmd.Parameters.AddWithValue("@importer", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@exporter", key);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                {
                    lblError.Text = "Fail.";
                    GridView1.DataBind();
                }
                else if (ret_code == -1)
                {
                    lblError.Text = "Done. But saving log failed!";
                    GridView1.DataBind();
                }
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                return;
            }
        }
    }
}